package eu.tanov.epf.pv.types.project.ui.pages;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.swt.SWT;

public class CheckBoxEditingSupport extends EditingSupport {

	private ColumnViewer viewer;

	public CheckBoxEditingSupport(ColumnViewer viewer) {
		super(viewer);
		this.viewer = viewer;
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
	}

	@Override
	protected boolean canEdit(Object element) {
		return true;
	}

	@Override
	protected Object getValue(Object element) {
		SelectableMethodPractice practice = (SelectableMethodPractice) element;
		return practice.isSelected();
	}

	@Override
	protected void setValue(Object element, Object value) {
		SelectableMethodPractice  practice = (SelectableMethodPractice ) element;
		Boolean isSelected = (Boolean) value;
		practice.setSelected(isSelected);
		viewer.refresh();
	}

}
